<template>
    <div class="page page_container">
        <div class="mask" v-if="shareState" @click="shareState = !shareState">
            <img src="../../../images/shareIndexTop.png"/>
        </div>
        <show-bg-img :isShow="coupList.length == 0" title="暂时没有优惠呢，去看看别的吧~~"></show-bg-img>
        <div class="card_wapper">
        <div class="public_list" v-for="(item,index) in coupList" :key="index">
           <div class="left">
                <div class="title">{{item.name | sliceString(17)}}</div>
                <div>{{item.thirdDesc |sliceString(15)}}</div>
                <div>数量：{{item.num}}</div>
                <div>{{item.validatyBegin}}至{{item.validatyEnd}}</div>
           </div>
           <div class="right">
                <div @click="share(item)">转让</div>
           </div>
        </div> 
        </div> 
       <div class="qr_code" v-if="qrshow">
            <div class="qrtitle">{{qrTitle}}</div>
            <img :src="imgurl" alt="">
       </div>
       <div class="Mask" @click="maskHide" v-if="maskShow"></div>
    </div>
</template>


<script>
import { Cell,MessageBox,Toast } from 'mint-ui';
import {queryShareCouponMore,shareQrCodeLink,queryShareCoupon,system,queryMyShareList,shareCouponLink} from "src/methods/request.js";
import { Common } from 'src/methods/common.js'
import { setWXConfig } from 'src/methods/baseinfo.js'
export default {
  // 最顶部黑色栏
  data () {
    return {
      integral:"",
      coupList:[],
      imgurl:"",
      maskShow:false,
      qrshow: false,
      shareState:false,
      qrTitle:"",
      link:""
    }
  },
  components: {
    
  },
  created(){
    setWXConfig(window.location.href.split('#')[0])
    this.$store.commit('increment',"批量转让")
    this.getquanlist();
  },
  methods:{
      share(item){
          this.qrTitle = item.name;
          MessageBox.prompt('请输入数量').then(({ value, action }) => {
              if(action=='confirm'){
                  if(!(/^([1-9][0-9]*){1,3}$/.test(value))){
                    Toast({message: '只能输入正整数！', position: 'center',duration: 1000 });
                    return
                  }
                  if(parseInt(value)>item.num){
                      Toast({message: '亲：卡包只有'+item.num+'张！', position: 'center',duration: 1000 });
                      return
                  }
                  let num=(value===null)?1:parseInt(value)
                  queryShareCouponMore({"userCouponNo":item.userCouponNo,"mchtId":item.mchtId,"shareCount":num}).then(
                response => {
                      if(response.rspCode == '0000'){
                        if(response.data.resCode == '0000'){
                            this.geterweima(response.data.shareCouponNo)//获取二维码
                        }else{
                            Toast({ message: response.data.message,position: 'middle',duration: 3000});
                        }
                      }else{
                            Toast({ message: '获取二维码失败',position: 'middle',duration: 3000});
                      }
                }).catch(error => console.log(error) ); 
              }
            });
              
      },
       //   微信分享
      getshareLink(shareCouponNo){
            var link = location.origin + '/ecshop/static/share.html?shareCouponNo=' + shareCouponNo;
            wx.onMenuShareAppMessage({
                title:"您收到一张好友分享的优惠券哦~",
                desc:"你的好友分享了一张优惠券给你，速速收下吧",
                link:link,
                imgUrl:"",
                type: 'link', // 分享类型,music、video或link，不填默认为link
                success: function () {
                    // alert("分享成功！")
                    this.shareState = true;
                    this.getquanlist();
                },
                cancel: function () {

                }
            })
      },
      geterweima(shareCouponNo){
            shareQrCodeLink({shareCouponNo:shareCouponNo}).then(response => {
                this.imgurl = 'data:image/png;base64,'+ response.data.QRcode;
                this.qrshow = true;
                this.maskShow = true;
            }).catch(error => console.log(error) );
      },
      goAddBank(){
           this.$router.push({
              path:"/addBank"
           })
      },
      maskHide(){
        this.qrshow = false;
        this.maskShow = false;
        this.getquanlist();
      },
      WXshare(item){
          MessageBox.prompt('请输入数量').then(({ value, action }) => {
              if(action=='confirm'){
                  if(parseInt(value)>item.num){
                      Toast({message: '亲：卡包只有'+item.num+'张！', position: 'center',duration: 1000 });
                      return
                  }
                 let num=(value===null)?1:parseInt(value)
                    queryShareCouponMore({"userCouponNo":item.userCouponNo,"mchtId":item.mchtId,"shareCount":num}).then(
                    response => {
                            if(response.rspCode == '0000'){
                                if(response.data.resCode == '0000'){
                                    const shareCouponNo = response.data.shareCouponNo
                                    this.shareState = true;
                                    this.getshareLink(shareCouponNo)
                                }else{
                                    Toast({ message: response.data.message,position: 'middle',duration: 3000});
                                }
                            }else{
                                Toast({ message: '获取分享码失败',position: 'middle',duration: 3000});
                            }
                }).catch(error => console.log(error) ); 
              }
            });
        
      },
    //   立即使用进入商品列表
      lijiuser(item){
          console.log(item)
           this.$router.push({
              path:"/category",
               query:{ couponNo:item.couponId,type:'1',cateName:"优惠商品",cateNo:"08" }
           })
      },
     //获取订单
     getquanlist(){
          this.$vux.loading.show({
                  text: 'Loading'
          })
          //获取列表
        queryMyShareList({"status":this.$route.query.types || '00'}).then(
                response => {
                      if(response.rspCode == '0000'){
                          this.coupList = response.data.couponList;
                      }
                      this.$vux.loading.hide()
                  }).catch(error => console.log(error) );
            setTimeout(() => {
              this.$vux.loading.hide()
            }, 2000)
        },
     },
     
};
</script>
<style lang="less" scoped>
@import "~src/components/css/gloState.less";
@import "./index.less";
</style>
